<?php
/**
 * Created by PhpStorm.
 * User: yhz
 * Date: 2017-12-22
 * Time: 00:30
 */

namespace app\common\model;

use app\base\BaseModel;

class AdminMenus extends BaseModel
{
    protected $table = 'tladmin_admin_menus';

    public function getMenuTree()
    {
        $menuData = $this->getList()->toArray();
        $treeData = $this->getTreeMenu($menuData, 0, 0);
        return $treeData;
    }

    public function getTreeMenu($data, $pid, $step)
    {
        global $tree;
        foreach ($data as $k => $v) {
            $data[$v['menu_id']] = $v;
        }
        foreach ($data as $key => $val) {
            if ($val['parent_id'] == $pid) {
                $flg = str_repeat('└―', $step);
                $val['title'] = $flg . $val['title'];
                $tree[] = $val;
                $this->getTreeMenu($data, $val['menu_id'], $step + 1);
            }
        }
        return $tree;
    }

    public function getTree1($list, $pk = 'id', $pid = 'pid', $child = 'child', $root = -1)
    {
        $tree = array();
        $packData = array();

        //将数组转换为索引数组
        foreach ($list as $item) {
            $packData[$item[$pk]] = $item;
        }
        foreach ($packData as $key => $value) {
            if ($value[$pid] == $root) {
                //根节点放入
                $tree[] =& $packData[$key];
            } else {
                //子节点放入
                $packData[$value[$pid]][$child][] =& $packData[$key];
            }
        }
        return $tree;
    }
}